package Program.AdminUI;

import Program.Information.Admin;
import Program.LoginUI.LoginUI;
import Program.SqlTool.AdminDAO;
import Program.Tool.Popup;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class AdminPaneSon extends JPanel {
    private AdminUI adminUI;
    private Admin admin;
    private JScrollPane jScrollPane;
    private JTable table;
    private DefaultTableModel dtm;
    private String[] titles = {"资料", "详细信息"};
    private Object[][] data;
    //删除按钮
    private JButton deleteJButton;

    public AdminPaneSon(Admin admin, AdminUI adminUI) {
        super(null);
        this.admin = admin;
        this.adminUI = adminUI;
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
        add(initScrollPane());
        add(initJButton());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(new ImageIcon("Application_images\\userBG1.png").getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
    }

    private JScrollPane initScrollPane() {
        jScrollPane = new JScrollPane();
        jScrollPane.getViewport().setOpaque(false);
        jScrollPane.setOpaque(false);
        jScrollPane.setBounds(120, 80, 650, 290);
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        JTable viewTable = getViewTable();
        jScrollPane.setViewportView(viewTable);
        return jScrollPane;
    }

    private JTable getViewTable() {
        data = createArr();
        dtm = new DefaultTableModel(data, titles);
        table = new JTable(dtm) {
            // 重写这个表格的方法：设置不可编辑，但可以选中
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 16));
        table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        table.setRowHeight(40);
        table.setRowHeight(4, 100);
        table.getTableHeader().setReorderingAllowed(false);// 设置列不可拖动
        // 设置居中
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JLabel.CENTER);
        renderer.setOpaque(false);
        table.setDefaultRenderer(Object.class, renderer);
        table.setOpaque(false);    // 设置表格本身透明
        table.getTableHeader().setOpaque(false);    //设置透明
        return table;
    }

    private Object[][] createArr() {
        Object[][] arr = {
                {"用户名", this.admin.getAdminName()},
                {"用户密码", this.admin.getAdminPassword()},
                {"姓名", this.admin.getName()},
                {"性别", this.admin.getAdminSex()},
                {"住址", "<html>" + this.admin.getAdminAddress() + "</html>"},
        };
        return arr;
    }

    private JButton initJButton() {
        deleteJButton = new JButton("删除账号");
        deleteJButton.setBounds(400, 420, 100, 60);
        deleteJButton.setBorder(null);
        deleteJButton.setFocusPainted(false);
        deleteJButton.setBackground(new Color(235, 135, 112));
        deleteJButton.setForeground(Color.WHITE);
        deleteJButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        deleteJButton.setToolTipText("Alt+delete可触发按钮");
        deleteJButton.setMnemonic(KeyEvent.VK_DELETE);
        deleteJButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 确认是否删除
                int get = JOptionPane.showOptionDialog(null, "请确认是否删除？", "友情询问", JOptionPane.YES_NO_OPTION,
                        JOptionPane.WARNING_MESSAGE, null, new Object[]{"是", "否"}, "否");
                if (get != 1) {
                    boolean b = AdminDAO.deleteAdmin(admin.getAdminName());
                    if (b) {
                        Popup.showJDialog("删除成功");
                        SwingUtilities.invokeLater(() -> new LoginUI());
                        adminUI.dispose();
                    }
                }else {
                    Popup.showJDialog("删除失败");
                }
            }
        });
        deleteJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        return deleteJButton;
    }

}
